import { Component, Injector } from '@angular/core';
import { IonicPage } from 'ionic-angular';
import { BasePage } from "../../basePage";
import { ActionSheetController } from 'ionic-angular';
import { nailerService } from '../../../providers/nailerService';
import { StorageService } from '../../../providers/StorageService';
import { PIC_URL } from "../../../providers/Config";

/**
 * Generated class for the PostPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-post',
  templateUrl: 'post.html',
})
export class PostPage extends BasePage {
  showPic: boolean;
  showSelect: boolean;
  imgarr = [];
  fileObjList: any;
  circle1Class = 'circle1';//选择图片
  lastImage = "";
  returnImg = "";
  msg: '';
  user_info: any;
  nailerName = '美甲栏目';
  publishObj = {
    article_title: '',
    article_content: '',
    article_tag: 0,
    presenter_type: 0,
    article_images: [],
    user_id: 0
  }
  picrooturl = '';
  deleteVo = {
    file: ''
  }
  constructor(injector: Injector, public actionSheetCtrl: ActionSheetController, public nailerservice: nailerService, public storage: StorageService) {
    super(injector);
    this.picrooturl = PIC_URL;
  }

  ionViewDidLoad() {
    this.user_info = this.storage.read('userinfo');
    this.publishObj.presenter_type = this.user_info['user_type'];
    this.publishObj.user_id = this.user_info['user_id'];
    console.log(this.user_info);
    this.showPic = false;
  }
  /**点击返回到上一页面 */
  dismissCLI() {
    let data = { 'foo': 'bar' };
    this.viewCtrl.dismiss(data);
  }

  presentToast() {
    let toast = this.toastCtrl.create({
      message: this.msg,
      duration: 2000,
      position: 'top'
    });
    toast.onDidDismiss(() => {
      console.log('Dismissed toast');
    });

    toast.present();
  }
  /**点击发布帖子*/
  async doPub() {
    console.log(this.publishObj);
    let res = await this.nailerservice.publishNearArticle(this.publishObj);
    if (res.error_code != 0) {
      this.msg = res.reason;
      this.presentToast();
    } else if (res.error_code == 0) {
      this.msg = res.msg;
      this.presentToast();
    }
    console.log(res);
  }
  presentActionSheet() {
    let actionSheet = this.actionSheetCtrl.create({
      title: '选择所属栏目',
      buttons: [
        {
          text: '美甲',
          handler: () => {
            this.nailerName = "美甲栏目";
            this.publishObj.article_tag = 1;
          }
        }, {
          text: '美睫',
          handler: () => {
            this.nailerName = "美睫栏目";
            this.publishObj.article_tag = 2;
          }
        },


        {
          text: '纹绣',
          handler: () => {
            this.nailerName = "纹绣栏目";
            this.publishObj.article_tag = 3;
          }
        }, {
          text: '微整形',
          handler: () => {
            this.nailerName = "微整形栏目";
            this.publishObj.article_tag = 4;
          }
        },


        {
          text: '取消',
          role: 'cancel',
          handler: () => {
            console.log('Cancel clicked');
          }
        }
      ]
    });
    actionSheet.present();
  }
  presentActionSheetCamr() {
    let actionSheet = this.actionSheetCtrl.create({
      title: '选择',
      buttons: [
        {
          text: '拍照上传',
          handler: () => {

          }
        }, {
          text: '从相册上传',
          handler: () => {

          }
        },

        {
          text: '取消',
          role: 'cancel',
          handler: () => {
            console.log('Cancel clicked');
          }
        }
      ]
    });
    actionSheet.present();
  }
  /**删除图片 */
  async delImg(picFile, index) {
    console.log(picFile)
    console.log(index)
    this.deleteVo.file = picFile;
    let res = await this.nailerservice.delImg(this.deleteVo);
    console.log(res)

    console.log(this.imgarr)
    if (res.code == 1) {
      this.toastCtrl.create({
        message: res.msg,
        duration: 3000,
        position: 'top'
      }).present();
      this.imgarr.splice(index, 1);
      this.publishObj.article_images = this.imgarr;
    }

  }

  /**子页面返回处理数据 */
  returnapiimg(event) {
    console.log("我是父页面");
    console.log(event);
    // this.userinfo = this.stc.read('userinfo');
    this.fileObjList = event[0];
    console.log(this.fileObjList)
    // console.log('我是data里面的数据' + this.fileObjList[0])
    // if (event.flagbs == 'circle1') {
    console.log('我后执行');
    // let serve1='assets/img/picbj.png'
    // var serve1 = this.fileObjList;
    // console.log(serve1)
    this.imgarr.push(this.fileObjList);
    this.publishObj.article_images = this.imgarr;
    if (this.imgarr.length >= 4) {
      this.showSelect = true;
    } else {
      this.showSelect = false;
    }
    if (this.imgarr.length < 4) {

      this.showPic = true;
    } else {
      this.showPic = false;
    }
  }
}
